package common.tree;


import org.json.JSONObject;

/**
 * 叶子节点类
 * @author lost
 */
public class EasyUITreeNode extends EasyUITreeElement{
	
	public static String STATE_OPEN = "open" ;
	public static String STATE_CLOSED = "closed" ;
	
	private JSONObject data ;
	private int id ;
	private String text ;
	private EasyUIForest children ;
	private String state ;
	private JSONObject attributes = null;
	private String menuUrl = null ;
	private int parentId = 0 ;
	private int displayNo = 0 ;
	
	/**
	 *  有的属性没有封装，需要到时候可以自己封装
	 *  http://www.jeasyui.com/documentation/index.php#
	 * id: An identity value bind to the node.
		text: Text to be showed.
		iconCls: The css class to display icon.
		checked: Whether the node is checked.
		state: The node state, 'open' or 'closed'.
		attributes: Custom attributes bind to the node.
		target: Target DOM object.
	 */
	public EasyUITreeNode(int id , String text){
		this.id = id ;
		this.text = text ;
		data = new JSONObject() ;
		children = new EasyUIForest() ;
		attributes = new JSONObject() ;
	}
	
	public void addChild(EasyUITreeNode node){
		children.add(node) ;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	
	public JSONObject getData(){
		data.put("id", this.id) ;
		data.put("text", this.text) ;
		if(state!=null)data.put("state", state) ;
		data.put("parentId", parentId) ;
		data.put("displayNo", displayNo) ;
		if(menuUrl!=null)data.put("menuUrl", menuUrl) ;
		if(children.getSize()!=0)
			data.put("children", children.getData()) ;
		if(attributes.keySet().size()!=0)
			data.put("attributes", attributes) ;
		return data ;
	}

	public void addAttribute(String key, String value) {
		try {
			attributes.put(key, value) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}

	public void removeAttribute(String key) {
		try {
			attributes.remove(key) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}

	protected JSONObject getAttribute() {
		return attributes ;
	}
	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getMenuUrl() {
		return menuUrl;
	}

	public void setMenuUrl(String menuUrl) {
		this.menuUrl = menuUrl;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public int getDisplayNo() {
		return displayNo;
	}

	public void setDisplayNo(int displayNo) {
		this.displayNo = displayNo;
	}
}